<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <title>用户登录</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../assets/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../assets/style/login.css" media="all">
    <link rel="stylesheet" href="../assets/style/admin.css">
    <script src="../assets/js/axios.min.js"></script>
    <script src="../assets/layui/layui.js"></script>
    <script src="../assets/js/request.js"></script>
    <script src="../assets/js/common.js"></script>
</head>
<body layadmin-themealias="default" class="layui-layout-body">
<div id="LAY_app" class="layadmin-tabspage-none">
    <div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login">
        <div class="layadmin-user-login-main">
            <div class="layadmin-user-login-box layadmin-user-login-header">
                <h2>用户登录</h2>
            </div>
            <form class="layui-form" action="">
                <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
                    <div class="layui-form-item">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-username"
                               for="LAY-user-login-username"></label>
                        <input type="text" name="username" autocomplete="true" id="LAY-user-login-username"
                               lay-verify="username" placeholder="用户名" class="layui-input">
                    </div>
                    <div class="layui-form-item">
                        <label class="layadmin-user-login-icon layui-icon layui-icon-password"
                               for="LAY-user-login-password"></label>
                        <input type="password" name="password" autocomplete="false" id="LAY-user-login-password"
                               lay-verify="password" placeholder="密码" class="layui-input">
                    </div>
                    <div class="layui-form-item">
                        <button class="layui-btn layui-btn-fluid" lay-submit="" lay-filter="LAY-user-login-submit">登 入
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>
    var isVerify = false
    layui.use('form', function () {
        var $ = layui.$, form = layui.form;

        form.render();

        form.verify({
            username: function (value) {
                if (value.length < 4 || value.length > 16) {
                    return '请输入用户名，4到16个字符';
                }
            },
            password: function (value) {
                if (value.length < 6 || value.length > 16) {
                    return '请输入密码，6到16个字符';
                }
            }
        });

        //提交
        form.on('submit(LAY-user-login-submit)', function (data) {
            layer.msg('提交中', {
                shadeClose: false
                , icon: 16
                , shade: 0.01
            });
            //请求登入接口
            service.get('auth?username=' + data.field.username + '&password=' + data.field.password)
                .then((response) => {
                    if (response.code === 200) {
                        //登入成功的提示与跳转
                        layer.msg('登入成功', {
                            offset: '15px'
                            , icon: 1
                            , time: 2000
                        }, function () {
                            // 将token缓存到本地
                            layui.data('TOKEN', {key: 'TOKEN', value: response.data.token})
                            layui.data('USERNAME', {key: 'USERNAME', value: response.data.username})
                            // 判断是否开启谷歌验证
                            service.get('mfconfig').then((config) => {
                                if (config.data) {
                                    var data = config.data
                                    data.forEach((e) => {
                                        if (e.id === 3) {
                                            isVerify = e.values === '1'
                                            /*10平台  20客服  30管理员*/
                                            if (response.data.role === 20) {
                                                location.href = '../cs_work/index.html'
                                            } else {
                                                if (isVerify && response.data.isbind === 10) { // 如果开启了google验证并且用户绑定了谷歌验证
                                                    location.href = '../account/accountInfo.html'
                                                } else {
                                                    location.href = '../account_manage/index.html'
                                                }
                                            }
                                        }
                                    })
                                } else {
                                    layer.msg(config.msg, {offset: '45vh', icon: 5, time: 2000});
                                }
                            }).catch((err) => {
                                layer.msg(err.msg, {offset: '45vh', icon: 5, time: 2000});
                            })
                        });
                    } else {
                        layer.msg(response.msg, {offset: '45vh', icon: 5, time: 2000});
                    }
                }).catch((err) => {
                layer.msg(err.msg, {offset: '45vh', icon: 5, time: 2000});
            })
            return false
        });
    })
</script>
</body>
</html>
